﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CamadaAcessoDadosPostgresql;
using Entidades;
using ControlesPadroes;

namespace InterfaceWinForm
{
    public partial class MenuDinamicoForm : WindowForm
    {
        private MenuForm _MenuPrincipal;
        private string _modulo;

        public string Modulo
        {
            get { return _modulo; }
            set { _modulo = value; }
        }
 
        public MenuDinamicoForm( string pModulo, MenuForm pMenuPrincipal )
        {
            InitializeComponent();
            Text = pModulo;
            Modulo = pModulo;
            MenuPrincipal = pMenuPrincipal;
        }

        public MenuForm MenuPrincipal
        {
            get { return _MenuPrincipal; }
            set { _MenuPrincipal = value; }
        }

        private void MenuDinamicoForm_Load(object sender, EventArgs e)
        {

            //    new MenuWork().ListaMenu;
            try
            {
                radTreeView1.DataSource = new MenuSistemaWork().ListaMenu(Modulo);
                radTreeView1.DisplayMember = "Descricao";
                radTreeView1.ValueMember = "Idmenu";
                radTreeView1.ParentIDMember = "Idmenupai";
                radTreeView1.CollapseAll();
            }
            catch (Exception ex)
            {
                MessageBox.Show(" Erro:" + ex.Message);
            }
        }



   

        private void radTreeView1_MouseClick(object sender, MouseEventArgs e)
        {

            MenuSistema menu;
            try
            {
                menu = new MenuSistemaWork().RetornaMenu((int)radTreeView1.SelectedNode.Tag);
            }
            catch
            {
                return;
            }

           // Object obj = new Form();
            if (menu.Objeto.Length > 2)            
                MenuPrincipal.CarregarObjeto(menu.Objeto, menu.Descricao);           
            
                       
        }
        

        private void radTreeView1_KeyDown(object sender, KeyEventArgs e)
        {
            if ((Control.ModifierKeys & Keys.Control) == Keys.Control && e.KeyCode == Keys.I)
            {
                // do whatever you want to do here...
                
                e.SuppressKeyPress = true;
            } 
        }

   
        #region Metodos do menu context

        private void alterarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenForm(EnumOpenForm.Alterar, false);
        }

        private void IncluirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenForm(EnumOpenForm.Incluir, false);
        }

        private void excluirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenForm(EnumOpenForm.Excluir, false);
        }

        private void incluirFilhoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenForm(EnumOpenForm.Incluir, true);
        }



        private void OpenForm(EnumOpenForm pOpenForm, bool pIncluirFilho)
        {
            string _tag = "";
            
            if (pOpenForm != EnumOpenForm.Incluir || pIncluirFilho)
            try
            {
                _tag = radTreeView1.SelectedNode.Tag.ToString();
            }
            catch
            {
                MessageBox.Show("Nenhum item selecionado no menu");
                return;
            }
            

            new MenuSistemaForm(_tag, pOpenForm).ShowDialog();
            

        }

        #endregion




      
    }
}
